/*
* IBM Confidential
* (C) Copyright IBM Corp. 2011 All Rights Reserved.
*/
package com.ibm.crm.tma.adapter.mq.testplan;

/**
 * <p>
 * This class is the default timer task provided by this
 * component which executes the aggregated TestOperation
 * implementation instance when running.
 * It extends BaseTimerTask to gain access to the TMA_MESSAGE_LOG
 * to be used for performing logging.
 * It overrides run() method from java.util.TimerTask in which it
 * calls testOperation.perform() method.
 * </p>
 * <p>
 * Thread Safety:
 * This class is thread safe as its base class is thread safe and
 * it adds no mutable attributes.
 * </p>
 * @author Schpotsky, TCSDEVELOPER
 * @version 1.0
 */
public class DefaultTimerTask extends BaseTimerTask {
    /**
     * <p>
     * An instance of TestOperation interface implementation which will
     * be executed when this task is running.
     * It is initialized in the constructor and never changed after that.
     * Can not be null.
     * It is not mutable.
     * It is used in run().
     * </p>
     */
    private final TestOperation testOperation;

    /**
     * <p>
     * Creates an instance of this class with the passed-in TestOperation
     * implementation instance.
     * </p>
     * @throws IllegalArgumentException
     *             If the input parameter is null.
     *
     * Implementation Notes :
     * this.testOperation = testOperation;
     * @param testOperation
     * An instance of TestOperation interface implementation
     * to be executed when this task is running.
     */
    public DefaultTimerTask(final TestOperation testOperation) {
        super();
        if (super.getTMAMessageLog() == null) {
            throw new IllegalArgumentException("TMA_MESSAGE_LOG "
                    + "cannot be null");
        }

        if (testOperation == null) {
            super.getTMAMessageLog().logError("TMA_PLAN_MANAGER_E"
                , DefaultTimerTask.class.getName(), "DefaultTimerTask"
                 + "(TestOperation testOperation)"
                , "Parameter TestOperation cannot be null");
            throw new IllegalArgumentException("Parameter TestOperation"
                    + " cannot be null");
        }

        this.testOperation = testOperation;
    }

    /**
     * <p>
     * Executes this task. It simply calls testOperation.perform().
     * </p>
     * Implementation Notes:
     *  // logging should be performed in this method as
     *  described in the section 1.3.1 of the CS.
     *
     * 1. testOperation.perform();
     */
    public void run() {
        super.getTMAMessageLog().logInformation("TMA_PLAN_MANAGER_I"
            , DefaultTimerTask.class.getName(), "run()"
            , "Default Timer Task is running to perform test operation.");
        try {
            testOperation.perform();
        } catch (TestOperationException e) {
            super.getTMAMessageLog().logError("TMA_PLAN_MANAGER_E"
                    , DefaultTimerTask.class.getName(), "run()"
                    , e.getLocalizedMessage());
        }
    }
}
